package site.zbox.tool.swing.xml.component;

import javax.swing.JComponent;
import javax.swing.JSplitPane;

import org.dom4j.Element;

import site.zbox.tool.lang.NumberUtils;
import site.zbox.tool.lang.StringUtils;
import site.zbox.tool.lang.Version;
import site.zbox.tool.swing.xml.IComponentParser;

@Version("2021.08.12")
public class SplitPaneComponentParser implements IComponentParser {
    @Override
    public String name() {
        return "SplitPane";
    }

    @Override
    public JComponent parse(Element element) {
        int orientation;
        if ("VERTICAL".equalsIgnoreCase(StringUtils.stripToEmpty(element.attributeValue("orientation")))) {
            orientation = JSplitPane.VERTICAL_SPLIT;
        } else {
            orientation = JSplitPane.HORIZONTAL_SPLIT;
        }
        final String resizeWeight = StringUtils.stripToEmpty(element.attributeValue("resize-weight"));
        final JSplitPane splitPane = new JSplitPane(orientation);
        if (StringUtils.isNotEmpty(resizeWeight)) {
            double value = NumberUtils.toDouble(resizeWeight);
            if (value < 0.0) {
                value = 0.0;
            }
            if (value > 1.0) {
                value = 1.0;
            }
            splitPane.setResizeWeight(value);
        }
        return splitPane;
    }
}
